<%-- 
    Document   : administracaoGruposEditar
    Created on : 12/11/2010, 12:53:25
    Author     : Thiago
--%>
<%@page import="dao.*"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="crypt.Crypt"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="bo.*"%>
<%@page import="beans.*"%>
<%@page import="autenticacao.Sessao"%>
<%
            String moduleId = "26";
            Crypt crypt = new Crypt();
            try {
                crypt.setKey(crypt.Decifragem((String) session.getAttribute("userPhrase")));
                String desafio = crypt.Cifragem(moduleId + "@" + ferramentas.DataHora.formatarDataHora(ferramentas.DataHora.retornarDataHoraAtual(), ferramentas.DataHora.DATE_TIME_FORMAT));
                if (!(new Sessao(session).validarSessao(desafio))) {
                    response.sendRedirect("/" + request.getContextPath());
                }
            } catch (Exception e) {
                response.sendRedirect("/" + request.getContextPath());
            }

            String id = (String) request.getParameter("id");

            TipoGrupoInspecao tgi = null;
            try {
                if ((id != null) && (!id.isEmpty())) {
                    tgi = new TipoGrupoInspecaoDAO().recuperarTipoGrupoInspecao(Integer.parseInt(id));
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

            if (tgi == null) {
                out.println("<script language=\"JavaScript\"> window.location.replace('" + (new String(request.getRequestURL()).split("pages")[0]) + "pages/administracaoGruposListar.jsp'); </script>");
                out.flush();
            }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Gestor Operacional</title>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/style.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/table.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/anytime.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/jquery.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytime.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytimetz.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/table.js"></script>
    </head>
    <body>
        <table width="100%">
            <tr>
                <td>
                    <jsp:include page = "../menu.jsp" flush = "true"/>
                </td>
            </tr>

            <tr align="center">
                <td><br/><br/>
                    <form name="form1" name="form1" method="post" action="administracaoGruposEditar.jsp">
                        <input type="hidden" name="action" value="executar"/>
                        <%

                                    String action = (String) request.getParameter("action");
                                    if ((action != null) && (action.equalsIgnoreCase("executar"))) {
                                        try {
                                            GrupoInspecao gi = new GrupoInspecao();
                                            gi.setTipoGrupoInspecao(tgi);
                                            new GrupoInspecaoDAO().excluirGrupoInspecao(gi);
                                            ArrayList<Atribuicao> aatrib = new AtribuicaoDAO().recuperarAtribuicao(null);
                                            for (int i = 0; i < aatrib.size(); i++) {
                                                if (Integer.valueOf((String) request.getParameter(aatrib.get(i).getColaborador().getMatricula())) == 0) {
                                                    gi.setColaborador(aatrib.get(i).getColaborador());
                                                    new GrupoInspecaoDAO().manterGrupoInspecao(gi);
                                                }
                                            }
                                            tgi.setSite(new Site());
                                            tgi.getSite().setId(Integer.parseInt((String)request.getParameter("site")));
                                            tgi.setDescricao((String) request.getParameter("descricao"));
                                            tgi.setColaborador(new Colaborador());
                                            tgi.getColaborador().setMatricula((String) request.getParameter("matricula"));
                                            tgi.setStatus(Integer.parseInt((String) request.getParameter("status")));
                                            new TipoGrupoInspecaoDAO().atualizarTipoGrupoInspecao(tgi);
                                            out.println("<strong><center>Atualização realizada com sucesso.</center></strong><br>");
                                        } catch (Exception e) {
                                            System.out.println(e.getMessage());
                                            out.println("<strong><center>Falha na atualização: " + e.getMessage() + ".</center></strong><br>");
                                        }
                                    }
                        %>
                        <table border="1" cellspacing="0" width="650" bgcolor="#FFFFFF">
                            <tr>
                                <td width="70%" colspan="2" align="center"><h3>Grupo: <% if (tgi != null) {
                                                out.println(tgi.getDescricao());
                                                out.println("<input type=\"hidden\" name=\"id\" value=\"" + tgi.getId() + "\">");
                                            }%></h3></td>
                            </tr>
                            <%
                                        if (tgi != null) {
                                            Atribuicao atrib = new Atribuicao();
                                            atrib.setSite(tgi.getSite());
                                            atrib.setStatus(Atribuicao.STS_ATIVO);
                                            ArrayList<Atribuicao> aatrib = new AtribuicaoDAO().recuperarAtribuicao(atrib);
                                            if ((aatrib != null) && (!aatrib.isEmpty())) {
                                                GrupoInspecao gi = new GrupoInspecao();
                                                gi.setTipoGrupoInspecao(tgi);
                                                ArrayList<GrupoInspecao> al = new GrupoInspecaoDAO().recuperarGrupoInspecao(gi);
                                                String[] colabs_in = new String[al.size()];
                                                for (int i = 0; i < al.size(); i++) {
                                                    if ((al.get(i).getColaborador() != null) && (al.get(i).getColaborador().getMatricula() != null) && (!al.get(i).getColaborador().getMatricula().isEmpty())) {
                                                        colabs_in[i] = al.get(i).getColaborador().getMatricula();
                                                        out.println("<tr>");
                                                        out.println("<td align=\"left\" width=\"70%\">" + al.get(i).getColaborador().getMatricula() + " - " + al.get(i).getColaborador().getNome() + "</td>");
                                                        out.println("<td align=\"center\"><input type=\"radio\" name=\"" + al.get(i).getColaborador().getMatricula() + "\" value=\"0\" checked>Sim   "
                                                                + "<input type=\"radio\" name=\"" + al.get(i).getColaborador().getMatricula() + "\" value=\"1\">Não</td>");
                                                        out.println("</tr>");
                                                    } else {
                                                        colabs_in[i] = "";
                                                    }
                                                }
                                                for (int i = 0; i < aatrib.size(); i++) {
                                                    if (ferramentas.Vetor.busca(colabs_in, aatrib.get(i).getColaborador().getMatricula()) == -1) {
                                                        out.println("<tr align=\"center\">");
                                                        out.println("<td align=\"left\" width=\"70%\">" + aatrib.get(i).getColaborador().getMatricula() + " - " + aatrib.get(i).getColaborador().getNome() + "</td><td align=\"center\">");
                                                        out.println("<input type=\"radio\" name=\"" + aatrib.get(i).getColaborador().getMatricula() + "\" value=\"0\">Sim   "
                                                                + "<input type=\"radio\" name=\"" + aatrib.get(i).getColaborador().getMatricula() + "\" value=\"1\" checked>Não");
                                                        out.println("</td></tr>");
                                                    }
                                                }
                                            }
                                        }
                            %>
                            <tr>
                                <td>
                                    Descrição:
                                </td>
                                <td align="center">
                                    <input name="descricao" type="text" value="<%
                                                if (tgi != null) {
                                                    out.print(tgi.getDescricao());
                                                }
                                           %>">
                                </td>
                            </tr>
                            <tr>
                                <td>Status:</td>
                                <td align="center">
                                    <select name="status" size="1">
                                        <%
                                                    if (tgi != null) {
                                                        if (tgi.getStatus() == Perfil.ATIVO) {
                                                            out.println("<option value=\"" + TipoGrupoInspecao.VLR_ATIVO + "\" selected>Ativo</option>");
                                                            out.println("<option value=\"" + TipoGrupoInspecao.VLR_INATIVO + "\">Inativo</option>");
                                                        } else {
                                                            out.println("<option value=\"" + TipoGrupoInspecao.VLR_INATIVO + "\" selected>Inativo</option>");
                                                            out.println("<option value=\"" + TipoGrupoInspecao.VLR_ATIVO + "\">Ativo</option>");
                                                        }
                                                    }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr><td>Site:</td>
                                <td align="center">
                                    <select name="site" size="1">
                                        <%
                                                    ArrayList<Site> als = null;
                                                    try {
                                                        als = new SiteBO().recuperarSite(null);
                                                    } catch (Exception e) {
                                                        out.println(e.getMessage());
                                                    }
                                                    if (als != null) {
                                                        for (int i = 0; i < als.size(); i++) {
                                                            if ((tgi != null)
                                                                    && (tgi.getSite() != null)
                                                                    && (tgi.getSite().getId() != Site.VLR_NAO_CONFIGURADO)
                                                                    && (tgi.getSite().getId() == als.get(i).getId())) {
                                                                out.println("<option value=\"" + als.get(i).getId() + "\" selected>" + als.get(i).getDescricao() + "</option>");
                                                            } else {
                                                                out.println("<option value=\"" + als.get(i).getId() + "\">" + als.get(i).getDescricao() + "</option>");
                                                            }
                                                        }
                                                    }

                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>Responsável:</td>
                                <td align="center">
                                    <select name="matricula" size="1">
                                        <%
                                                    ArrayList<Colaborador> alc = null;
                                                    try {
                                                        alc = new ColaboradorBO().recuperarColaborador(null);
                                                    } catch (Exception e) {
                                                        out.println(e.getMessage());
                                                    }
                                                    if (alc != null) {
                                                        for (int i = 0; i < alc.size(); i++) {
                                                            String aux = "";
                                                            if ((tgi != null) && alc.get(i).getMatricula().equalsIgnoreCase(tgi.getColaborador().getMatricula())) {
                                                                aux = " selected";
                                                            }
                                                            out.println("<option value=\"" + alc.get(i).getMatricula() + "\"" + aux + ">" + alc.get(i).getMatricula() + " - " + alc.get(i).getNome() + "</option>");
                                                        }
                                                    }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="center">
                                    <input type="submit" value="Atualizar">
                                </td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr>
        </table>
    </body>
</html>